package sys_serv

import (
	"strconv"
	"strings"
	"ubt-aio-service/app/mod/sys_mod"
	"ubt-aio-service/app/pkg/db"
	"ubt-aio-service/app/utils"
)

type feedback struct{}

var FeedbackServ = feedback{}

// Add 新增配置
func (s *feedback) Add(req *sys_mod.FeedbackSaveReq) *utils.ResJson {
	sqlStr := "insert into sys_feedback (fb_user, phone, email, img_ids, img_names, img_urls, fb_type, kind, parent_id, remarks, states, add_time, upd_time) " +
		"VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, 1, EXTRACT(epoch FROM NOW()), EXTRACT(epoch FROM NOW()))"
	return db.CudData(sqlStr, req.FbUser, req.Phone, req.Email, req.ImgIds, req.ImgNames, req.ImgUrls, req.FbType, req.Kind, req.ParentId, req.Remarks)
}

// Update 修改配置
func (s *feedback) Update(req *sys_mod.FeedbackSaveReq) *utils.ResJson {
	sqlStr := `UPDATE sys_feedback SET fb_user=$1, phone=$2, email=$3, img_ids=$4, img_names=$5, img_urls=$6, fb_type=$7, remarks=$8, upd_time=EXTRACT(epoch FROM NOW()) WHERE fb_id=$9`
	return db.CudData(sqlStr, req.FbUser, req.Phone, req.Email, req.ImgIds, req.ImgNames, req.ImgUrls, req.FbType, req.Remarks, req.FbId)
}

// Process 处理配置
func (s *feedback) Process(req *sys_mod.FeedbackProReq) *utils.ResJson {
	var (
		param []any
		q     string
		n     = 1
	)
	param = append(param, req.States)
	idList := strings.Split(req.FbIds, ",")
	for _, curId := range idList {
		n++
		q += "$" + strconv.Itoa(n) + ","
		param = append(param, curId)
	}
	q = q[:len(q)-1]
	sqlStr := "UPDATE sys_feedback SET states=$1, upd_time=EXTRACT(epoch FROM NOW()) WHERE fb_id IN (" + q + ")"
	return db.CudData(sqlStr, param...)
}

// One 获取配置详情
func (s *feedback) One(fbId string) *utils.ResJson {
	sqlStr := "SELECT a.fb_id, a.fb_user, a.phone, a.email, a.img_ids, a.img_names, a.img_urls, a.fb_type, b.dict_name AS type_name, b.dict_code AS type_code, a.kind, a.parent_id, a.remarks, a.states, a.add_time, a.upd_time " +
		"FROM sys_feedback a " +
		"LEFT JOIN sys_dict b ON b.states=1 AND b.kind=2 AND b.type_code='FEEDBACK' AND a.fb_type=b.dict_id " +
		"WHERE a.fb_id=$1"
	return db.GetOne(sqlStr, fbId)
}

// Count 获取配置数量
func (s *feedback) Count(req *sys_mod.FeedbackPageReq) *utils.ResJson {
	sqlStr := "SELECT COUNT(1) AS nums FROM sys_feedback WHERE states IN (1, 3) AND kind=1"
	param := make([]any, 0)
	var n = 0
	if req.Keywords != "" {
		sqlStr += " AND ("
		nameArr := strings.Split(req.Keywords, " ")
		for _, name := range nameArr {
			sqlStr += "phone LIKE '%'||$" + strconv.Itoa(n+1) + "||'%' OR " +
				"email LIKE '%'||$" + strconv.Itoa(n+2) + "||'%' OR " +
				"remarks LIKE '%'||$" + strconv.Itoa(n+3) + "||'%' OR "
			param = append(param, name)
			param = append(param, name)
			param = append(param, name)
			n += 3
		}
		sqlStr = sqlStr[:len(sqlStr)-4]
		sqlStr += ")"
	}
	if req.FbType != "" {
		sqlStr += " AND fb_type=$" + strconv.Itoa(n+1)
		param = append(param, req.FbType)
		n++
	}
	if res := db.GetOne(sqlStr, param...); res.Code == utils.Succeed.Code && res.Data.(map[string]any)["nums"].(int64) > 0 {
		return res
	}
	return utils.NoData
}

// Page 获取配置分页列表
func (s *feedback) Page(req *sys_mod.FeedbackPageReq) *utils.ResJson {
	sqlStr := "SELECT a.fb_id, b.nick_name, a.phone, a.email, a.img_ids, a.img_names, a.img_urls, a.fb_type, a.parent_id, a.remarks, a.states, a.add_time " +
		"FROM sys_feedback a " +
		"LEFT JOIN sys_user b ON b.states=1 AND a.fb_user=b.user_id " +
		"LEFT JOIN sys_dict c ON c.states=1 AND c.kind=2 AND c.type_code='FEEDBACK' AND a.fb_type=c.dict_id " +
		"WHERE a.states IN (1,3) AND a.kind=1"
	param := make([]any, 0)
	var n = 0
	if req.Keywords != "" {
		sqlStr += " AND ("
		nameArr := strings.Split(req.Keywords, " ")
		for _, name := range nameArr {
			sqlStr += "a.phone LIKE '%'||$" + strconv.Itoa(n+1) + "||'%' OR " +
				"a.email LIKE '%'||$" + strconv.Itoa(n+2) + "||'%' OR " +
				"a.remarks LIKE '%'||$" + strconv.Itoa(n+3) + "||'%' OR "
			param = append(param, name)
			param = append(param, name)
			param = append(param, name)
			n += 3
		}
		sqlStr = sqlStr[:len(sqlStr)-4]
		sqlStr += ")"
	}
	if req.FbType != "" {
		sqlStr += " a.AND fb_type=$" + strconv.Itoa(n+1)
		param = append(param, req.FbType)
		n++
	}
	sqlStr += " ORDER BY a.add_time DESC LIMIT $" + strconv.Itoa(n+1) + " OFFSET $" + strconv.Itoa(n+2)
	param = append(param, req.Limit)
	param = append(param, req.Offset)
	return db.GetAll(sqlStr, param...)
}

// Replied 获取回复的信息
func (s *feedback) Replied(parentId string) *utils.ResJson {
	sqlStr := "SELECT a.fb_id, b.nick_name, a.img_ids, a.img_names, a.img_urls, a.remarks, a.add_time " +
		"FROM sys_feedback a " +
		"LEFT JOIN sys_user b ON b.states=1 AND a.fb_user=b.user_id " +
		"WHERE a.states IN (1, 3) AND a.kind=2 AND a.parent_id=$1 ORDER BY a.add_time"
	return db.GetAll(sqlStr, parentId)
}
